package com.fanshilz.flower2025.apps.admin.browse.controller;

import com.fanshilz.flower2025.apps.admin.browse.req.ProjectBrowseTableReq;
import com.fanshilz.flower2025.apps.admin.browse.resp.ProjectBrowseTableDetailResp;
import com.fanshilz.flower2025.business.flower.projectBrowse.service.IProjectBrowseService;
import com.fanshilz.flower2025.framework.resp.Resp;
import com.fanshilz.flower2025.framework.resp.TableResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.fanshilz.flower2025.business.flower.projectBrowse.entity.table.ProjectBrowseTableDef.PROJECT_BROWSE;

@RestController
@RequestMapping("/admin/browse")
public class BrowseController {

    @Resource
    private IProjectBrowseService projectBrowseService;

    @PostMapping("/table")
    public Resp<TableResp<ProjectBrowseTableDetailResp>> table(@RequestBody @Valid ProjectBrowseTableReq req) {
        QueryWrapper qw = QueryWrapper.create();
        qw.orderBy(PROJECT_BROWSE.BROWSE_ID.desc());
        Page<ProjectBrowseTableDetailResp> page = projectBrowseService.getMapper()
                .paginateWithRelationsAs(Page.of(req.getCurrent(), req.getPageSize(), req.getTotal()), qw, ProjectBrowseTableDetailResp.class);
        return Resp.success(new TableResp<>(page.getRecords(), page.getTotalRow()));
    }

}
